from flask import Flask,json, jsonify
from app.machines.cpu import CpuInfo
from app.machines.mac import MacInfo
import json
from app.database.connections import hapi,db
from app.models.models import Cpu
from app.models.model_memory import Memory
from app.models.model_disk import Disk
from app.models.model_network import Network
from app.models.model_machine import Machine

from flask import request
import datetime
import time

# app = Flask(__name__)


@hapi.route('/api', methods=['POST','GET'])
def api():
    global maccode
    global hardwarecode
    if request.method == "POST":
        maccode = request.form['maccode']
        hardwarecode = request.form['hardwarecode']
    else:
        maccode = request.args['maccode']
        hardwarecode = request.args['hardwarecode']
    j = ''
    if "cpu" == hardwarecode.lower():
        j = query_cpu(maccode)
    elif "memory" == hardwarecode.lower():
        j = query_memory(maccode)
    elif "disk" == hardwarecode.lower():
        j = query_disk(maccode)
    elif "network" == hardwarecode.lower():
        j = query_network(maccode)
    elif "machine" == hardwarecode.lower():
        j = query_machine()
    return j

def query_cpu(maccode):
    print("@@@@" + maccode)
    data = db.session.query(Cpu).filter(Cpu.mac_code==maccode).order_by(Cpu.id.desc()).first()
    print(data)
    return cpu_to_json(data)

def cpu_to_json(data):
    print(data.uptimes)
    cpu_dict = {}
    cpu_dict['id'] = data.id
    cpu_dict['cpu_name'] = data.cpu_name
    cpu_dict['utilization'] = str(data.utilization)
    cpu_dict['logical_processors'] = data.logical_processors
    cpu_dict['speed'] = str(data.speed)
    cpu_dict['base_speed'] = str(data.base_speed)
    cpu_dict['processes'] = data.processes
    cpu_dict['uptimes'] = str(data.uptimes)
    cpu_dict['logical_processors'] = data.logical_processors
    cpu_dict['kernels'] = data.kernels
    cpu_dict['handles'] = data.handles
    cpu_dict['threads'] = data.threads
    cpu_dict['mac_code'] = data.mac_code
    j = json.dumps(cpu_dict)
    return j

def query_memory(maccode):
    print("###" + maccode)
    data = db.session.query(Memory).filter(Memory.mac_code == maccode).order_by(Memory.id.desc()).first()
    return memory_to_json(data)

def memory_to_json(data):
    memory_dict = {}
    memory_dict['id'] = data.id
    memory_dict['mac_code'] = data.mac_code
    memory_dict['total_memory_size'] = str(data.total_memory_size)
    memory_dict['available_size'] = str(data.available_size)
    memory_dict['used_percent'] = str(data.used_percent)
    memory_dict['used_size'] = str(data.used_size)
    memory_dict['memory_usage'] = str(data.memory_usage)
    memory_dict['spare_size'] = str(data.spare_size)
    memory_dict['swap_memory_total'] = str(data.swap_memory_total)
    memory_dict['swap_used_percent'] = str(data.swap_used_percent)
    memory_dict['swap_used_size'] = str(data.swap_used_size)
    memory_dict['swap_memory_usage'] = str(data.swap_memory_usage)
    memory_dict['swap_spare_size'] = str(data.swap_spare_size)
    memory_dict['swap_sin'] = str(data.swap_sin)
    memory_dict['swap_sout'] = str(data.swap_sout)

    j = json.dumps(memory_dict)
    return j

def query_disk(maccode):
    print("###" + maccode)
    data = db.session.query(Disk).filter(Disk.mac_code == maccode).order_by(Disk.id.desc()).first()
    print(data)
    return disk_to_json(data)

def disk_to_json(data):
    disk_dict = {}
    disk_dict['id'] = data.id
    disk_dict['mac_code'] = data.mac_code
    disk_dict['disk_name'] = data.disk_name
    disk_dict['disk_parts'] = data.disk_parts
    disk_dict['disk_total_size'] = str(data.disk_total_size)
    disk_dict['disk_used'] = str(data.disk_used)
    disk_dict['disk_free'] = str(data.disk_free)
    disk_dict['disk_use_percent'] = str(data.disk_use_percent)
    disk_dict['io_read_count'] = str(data.io_read_count)
    disk_dict['io_write_count'] = str(data.io_write_count)
    disk_dict['io_read_size'] = str(data.io_read_size)
    disk_dict['io_write_size'] = str(data.io_write_size)
    disk_dict['io_read_time'] = str(data.io_read_time)
    disk_dict['io_write_time'] = str(data.io_write_time)

    j = json.dumps(disk_dict)
    return j

def query_network(maccode):
    print("###" + maccode)
    data = db.session.query(Network).filter(Network.mac_code == maccode).order_by(Network.id.desc()).first()
    return network_to_json(data)

def network_to_json(data):
    network_dict = {}
    network_dict['id'] = data.id
    network_dict['mac_code'] = data.mac_code
    network_dict['network_name'] = data.network_name
    network_dict['bytes_sent'] = str(data.bytes_sent)
    network_dict['bytes_recv'] = str(data.bytes_recv)
    network_dict['packets_sent'] = data.packets_sent
    network_dict['packets_recv'] = data.packets_recv
    network_dict['errin'] = data.errin
    network_dict['errout'] = data.errout
    network_dict['dropin'] = data.dropin
    network_dict['dropout'] = data.dropout

    j = json.dumps(network_dict)
    return j

def query_machine():
    datalist = db.session.query(Machine).all()
    print(datalist)
    return machine_to_json(datalist)

def machine_to_json(datalist):
    machine_list = []
    for data in datalist:
        machine_dict = {}
        machine_dict['id'] = data.id
        machine_dict['mac_code'] = data.mac_code
        machine_dict['machine_name'] = data.machine_name
        machine_list.append(machine_dict)
    j = json.dumps(machine_list)
    return j

if __name__ == '__main__':
    hapi.run(debug=True,host='0.0.0.0',port=9000)
